rm(list=ls())
setwd("D:/Dropbox/Grad School Year 3 Term 1/Time Series/TS Project")


load("Time_Series-Original.RData")

library(KRMSBVAR)
library(coda)
library(RColorBrewer)
library(coda)
library(LSTS)


residual.check <- function(residuals, selection=1){
  
  print(Box.test(residuals[,selection], 14))
  par(mfrow=c(2,2))
  acf(residuals[,selection])
  pacf(residuals[,selection])
  LSTS::Box.Ljung.Test(residuals[,selection], 14)
}

##### Set Priors here

lambda0 <- .9 # believe the sample variance reflects well the error 
lambda1 <- .5# believe relativel persistence
lambda3 <- .25 # relative slow decrease in effects
lambda4 <- .25 #
lambda5 <- .20 # 
mu5 <- 5
mu6 <- 5
priors <- list(l0=lambda0, l1=lambda1, l3=lambda3, l4=lambda4, l5=lambda5, m5=mu5, m6=mu6)
### Set number of IDs (not including the base one)
# anov
#Recusirve
base.id <- matrix(c(1,0,0, 1, 1, 0, 1,1, 1), nrow=3, ncol=3)
colnames(base.id) <- colnames(trump)
rownames(base.id) <- colnames(trump)



set.seed(1)
mods <- szbsvar(trump, p=7, rep.debate[,2:8], lambda0=priors$l0,lambda1=priors$l1,
                      lambda3=priors$l3, lambda4=priors$l4, lambda5=priors$l5,
                      mu5=priors$m5, mu6=priors$m6, ident=base.id)


residual.check(mods$structural.innovations, 1)
residual.check(mods$structural.innovations, 2)
residual.check(mods$structural.innovations, 3)

######## residuals are white noise, yay!
######## Now make IRFs for Trump

set.seed(1)
posts <- gibbs.A0(mods, N1=4000, N2=40000)
irf <- mc.irf(mods, A0.posterior = posts, nsteps=5, 
                    draws=1000, sign.list = sign(diag(mods$A0.mode)), scale=T)

trump.irf <- plot(irf, probs=c(0.025,0.975))


trump.irf$eigenvector.fractions
#### eigen vectors are close to 100 in first component. 


coda.a0 <- A02mcmc(posts)
plot(coda.a0, ask=F)
autocorr.plot(coda.a0)
geweke.diag(coda.a0, frac1=0.1, frac2=0.5)



set.seed(1)
mods <- szbsvar(bush, p=10, rep.debate[,2:8], lambda0=priors$l0,lambda1=priors$l1,
                lambda3=priors$l3, lambda4=priors$l4, lambda5=priors$l5,
                mu5=priors$m5, mu6=priors$m6,  ident=(base.id))


residual.check(mods$structural.innovations, 1)
residual.check(mods$structural.innovations, 2)
residual.check(mods$structural.innovations, 3)

#### white noise

set.seed(1)
posts <- gibbs.A0(mods, N1=4000, N2=40000)
irf <- mc.irf(mods, A0.posterior = posts, nsteps=5, 
              draws=1000, sign.list = sign(diag(mods$A0.mode)), scale=T)

bush.irf <- plot(irf, probs=c(0.025,0.975))
bush.irf$eigenvector.fractions

coda.a0 <- A02mcmc(posts)
plot(coda.a0, ask=F)
autocorr.plot(coda.a0)
geweke.diag(coda.a0, frac1=0.1, frac2=0.5)
###### good looking traceplots and autocrrelations 



set.seed(1)
mods <- szbsvar(carson, p=10, rep.debate[,2:8], lambda0=priors$l0,lambda1=priors$l1,
                lambda3=priors$l3, lambda4=priors$l4, lambda5=priors$l5,
                mu5=priors$m5, mu6=priors$m6,  ident=(base.id))


residual.check(mods$structural.innovations, 1)
residual.check(mods$structural.innovations, 2)
residual.check(mods$structural.innovations, 3)

### white noise

set.seed(1)
posts <- gibbs.A0(mods, N1=4000, N2=40000)
irf <- mc.irf(mods, A0.posterior = posts, nsteps=5, 
              draws=1000, sign.list = sign(diag(mods$A0.mode)), scale=T)

carson.irf <- plot(irf, probs=c(0.025,0.975))
carson.irf$eigenvector.fractions


coda.a0 <- A02mcmc(posts)
plot(coda.a0, ask=F)
autocorr.plot(coda.a0)
geweke.diag(coda.a0, frac1=0.1, frac2=0.5)



set.seed(1)
mods <- szbsvar(christie, p=7, rep.debate[,2:8], lambda0=priors$l0,lambda1=priors$l1,
                lambda3=priors$l3, lambda4=priors$l4, lambda5=priors$l5,
                mu5=priors$m5, mu6=priors$m6,  ident=(base.id))


residual.check(mods$structural.innovations, 1)
residual.check(mods$structural.innovations, 2)
residual.check(mods$structural.innovations, 3)

##### white noise

set.seed(1)
posts <- gibbs.A0(mods, N1=4000, N2=40000)
irf <- mc.irf(mods, A0.posterior = posts, nsteps=5, 
              draws=1000, sign.list = sign(diag(mods$A0.mode)), scale=T)

christie.irf <- plot(irf, probs=c(0.025,0.975))
christie.irf$eigenvector.fractions

coda.a0 <- A02mcmc(posts)
plot(coda.a0, ask=F)
autocorr.plot(coda.a0)
geweke.diag(coda.a0, frac1=0.1, frac2=0.5)
####### convergence 


set.seed(1)
mods <- szbsvar(cruz, p=7, rep.debate[,2:8], lambda0=priors$l0,lambda1=priors$l1,
                lambda3=priors$l3, lambda4=priors$l4, lambda5=priors$l5,
                mu5=priors$m5, mu6=priors$m6,  ident=(base.id))


residual.check(mods$structural.innovations, 1)
residual.check(mods$structural.innovations, 2)
residual.check(mods$structural.innovations, 3)

##### white noise

set.seed(1)
posts <- gibbs.A0(mods, N1=4000, N2=40000)
irf <- mc.irf(mods, A0.posterior = posts, nsteps=5, 
              draws=1000, sign.list = sign(diag(mods$A0.mode)), scale=T)

cruz.irf <- plot(irf, probs=c(0.025,0.975))
cruz.irf$eigenvector.fractions


coda.a0 <- A02mcmc(posts)
plot(coda.a0, ask=F)
autocorr.plot(coda.a0)
geweke.diag(coda.a0, frac1=0.1, frac2=0.5)
########## convergence


set.seed(1)
mods <- szbsvar(fiorina, p=7, rep.debate[,2:8], lambda0=priors$l0,lambda1=priors$l1,
                lambda3=priors$l3, lambda4=priors$l4, lambda5=priors$l5,
                mu5=priors$m5, mu6=priors$m6,  ident=(base.id))


residual.check(mods$structural.innovations, 1)
residual.check(mods$structural.innovations, 2)
residual.check(mods$structural.innovations, 3)

## white noise 


set.seed(1)
posts <- gibbs.A0(mods, N1=4000, N2=40000)
irf <- mc.irf(mods, A0.posterior = posts, nsteps=5, 
              draws=1000, sign.list = sign(diag(mods$A0.mode)), scale=T)

fiorina.irf <- plot(irf, probs=c(0.025,0.975))
fiorina.irf$eigenvector.fractions

coda.a0 <- A02mcmc(posts)
plot(coda.a0, ask=F)
autocorr.plot(coda.a0)
geweke.diag(coda.a0, frac1=0.1, frac2=0.5)
#### Convergence


set.seed(1)
mods <- szbsvar(paul, p=7, rep.debate[,2:8], lambda0=priors$l0,lambda1=priors$l1,
                lambda3=priors$l3, lambda4=priors$l4, lambda5=priors$l5,
                mu5=priors$m5, mu6=priors$m6,  ident=(base.id))


residual.check(mods$structural.innovations, 1)
residual.check(mods$structural.innovations, 2)
residual.check(mods$structural.innovations, 3)

## white noise

set.seed(1)
posts <- gibbs.A0(mods, N1=4000, N2=40000)
irf <- mc.irf(mods, A0.posterior = posts, nsteps=5, 
              draws=1000, sign.list = sign(diag(mods$A0.mode)), scale=T)

paul.irf <- plot(irf, probs=c(0.025,0.975))
paul.irf$eigenvector.fractions

coda.a0 <- A02mcmc(posts)
plot(coda.a0, ask=F)
autocorr.plot(coda.a0)
geweke.diag(coda.a0, frac1=0.1, frac2=0.5)
#################### 
#convergence


set.seed(1)
mods <- szbsvar(rubio, p=7, rep.debate[,2:8], lambda0=priors$l0,lambda1=priors$l1,
                lambda3=priors$l3, lambda4=priors$l4, lambda5=priors$l5,
                mu5=priors$m5, mu6=priors$m6,  ident=(base.id))


residual.check(mods$structural.innovations, 1)
residual.check(mods$structural.innovations, 2)
residual.check(mods$structural.innovations, 3)

## white noise

set.seed(1)
posts <- gibbs.A0(mods, N1=4000, N2=40000)
irf <- mc.irf(mods, A0.posterior = posts, nsteps=5, 
              draws=1000, sign.list = sign(diag(mods$A0.mode)), scale=T)

rubio.irf <- plot(irf, probs=c(0.025,0.975))
rubio.irf$eigenvector.fractions

coda.a0 <- A02mcmc(posts)
plot(coda.a0, ask=F)
autocorr.plot(coda.a0)
geweke.diag(coda.a0, frac1=0.1, frac2=0.5)
## convergence 



###### ORdering of responses 
##### 1 4 7
##### 2 5 8
##### 3 6 9
##### media -> media : 1
##### media -> interest : 2
##### media -> polling : 3
##### interest -> media : 4
##### interest -> support: 6
##### support -> media: 7

setwd("D:/Dropbox/Grad School Year 3 Term 1/Time Series/TS Project/PoP Conditional Accept")

pdf("Media_on_Support.pdf", height=12, width=8)
# png("Media_on_Support.png", height=12, width=8, unit="in", res=120)
par(mfcol=c(4,2))

plot.new()
plot.window(xlim=c(1,5), ylim=c(-10,20))
polygon(x=c(1:5,5:1), y=c(trump.irf$responses[,2,3], rev(trump.irf$responses[,1,3])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(trump.irf$responses[,3,3], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Trump", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-10,20))
polygon(x=c(1:5,5:1), y=c(bush.irf$responses[,2,3], rev(bush.irf$responses[,1,3])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(bush.irf$responses[,3,3], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Bush", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-10,20))
polygon(x=c(1:5,5:1), y=c(carson.irf$responses[,2,3], rev(carson.irf$responses[,1,3])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(carson.irf$responses[,3,3], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Carson", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-10,20))
polygon(x=c(1:5,5:1), y=c(christie.irf$responses[,2,3], rev(christie.irf$responses[,1,3])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(christie.irf$responses[,3,3], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Christie", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-10,20))
polygon(x=c(1:5,5:1), y=c(cruz.irf$responses[,2,3], rev(cruz.irf$responses[,1,3])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(cruz.irf$responses[,3,3], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Cruz", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-10,20))
polygon(x=c(1:5,5:1), y=c(fiorina.irf$responses[,2,3], rev(fiorina.irf$responses[,1,3])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(fiorina.irf$responses[,3,3], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Fiorina", xlab="Days", ylab="Change in Polling")



plot.new()
plot.window(xlim=c(1,5), ylim=c(-10,20))
polygon(x=c(1:5,5:1), y=c(paul.irf$responses[,2,3], rev(paul.irf$responses[,1,3])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(paul.irf$responses[,3,3], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Paul", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-10,20))
polygon(x=c(1:5,5:1), y=c(rubio.irf$responses[,2,3], rev(rubio.irf$responses[,1,3])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(rubio.irf$responses[,3,3], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Rubio", xlab="Days", ylab="Change in Polling")
dev.off()










setwd("D:/Dropbox/Grad School Year 3 Term 1/Time Series/TS Project/PoP Conditional Accept")

pdf("Media_on_Interest.pdf", height=12, width=8)
# png("Media_on_Interest.png",  height=12, width=8, unit="in", res=120)
par(mfcol=c(4,2))

plot.new()
plot.window(xlim=c(1,5), ylim=c(-1,5))
polygon(x=c(1:5,5:1), y=c(trump.irf$responses[,2,2], rev(trump.irf$responses[,1,2])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(trump.irf$responses[,3,2], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Trump", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-1,5))
polygon(x=c(1:5,5:1), y=c(bush.irf$responses[,2,2], rev(bush.irf$responses[,1,2])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(bush.irf$responses[,3,2], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Bush", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-1,5))
polygon(x=c(1:5,5:1), y=c(carson.irf$responses[,2,2], rev(carson.irf$responses[,1,2])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(carson.irf$responses[,3,2], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Carson", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-1,5))
polygon(x=c(1:5,5:1), y=c(christie.irf$responses[,2,2], rev(christie.irf$responses[,1,2])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(christie.irf$responses[,3,2], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Christie", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-1,5))
polygon(x=c(1:5,5:1), y=c(cruz.irf$responses[,2,2], rev(cruz.irf$responses[,1,2])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(cruz.irf$responses[,3,2], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Cruz", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-1,5))
polygon(x=c(1:5,5:1), y=c(fiorina.irf$responses[,2,2], rev(fiorina.irf$responses[,1,2])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(fiorina.irf$responses[,3,2], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Fiorina", xlab="Days", ylab="Change in Polling")



plot.new()
plot.window(xlim=c(1,5), ylim=c(-1,5))
polygon(x=c(1:5,5:1), y=c(paul.irf$responses[,2,2], rev(paul.irf$responses[,1,2])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(paul.irf$responses[,3,2], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Paul", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-1,5))
polygon(x=c(1:5,5:1), y=c(rubio.irf$responses[,2,2], rev(rubio.irf$responses[,1,2])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(rubio.irf$responses[,3,2], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Rubio", xlab="Days", ylab="Change in Polling")
dev.off()









setwd("D:/Dropbox/Grad School Year 3 Term 1/Time Series/TS Project/PoP Conditional Accept")

pdf("Interest_on_Media.pdf", height=12, width=8)
# png("Interest_on_Media.png",  height=12, width=8, unit="in", res=120)
par(mfcol=c(4,2))

plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,2))
polygon(x=c(1:5,5:1), y=c(trump.irf$responses[,2,4], rev(trump.irf$responses[,1,4])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(trump.irf$responses[,3,4], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Trump", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,2))
polygon(x=c(1:5,5:1), y=c(bush.irf$responses[,2,4], rev(bush.irf$responses[,1,4])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(bush.irf$responses[,3,4], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Bush", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,2))
polygon(x=c(1:5,5:1), y=c(carson.irf$responses[,2,4], rev(carson.irf$responses[,1,4])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(carson.irf$responses[,3,4], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Carson", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,2))
polygon(x=c(1:5,5:1), y=c(christie.irf$responses[,2,4], rev(christie.irf$responses[,1,4])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(christie.irf$responses[,3,4], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Christie", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,2))
polygon(x=c(1:5,5:1), y=c(cruz.irf$responses[,2,4], rev(cruz.irf$responses[,1,4])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(cruz.irf$responses[,3,4], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Cruz", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,2))
polygon(x=c(1:5,5:1), y=c(fiorina.irf$responses[,2,4], rev(fiorina.irf$responses[,1,4])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(fiorina.irf$responses[,3,4], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Fiorina", xlab="Days", ylab="Change in Polling")



plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,2))
polygon(x=c(1:5,5:1), y=c(paul.irf$responses[,2,4], rev(paul.irf$responses[,1,4])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(paul.irf$responses[,3,4], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Paul", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,2))
polygon(x=c(1:5,5:1), y=c(rubio.irf$responses[,2,4], rev(rubio.irf$responses[,1,4])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(rubio.irf$responses[,3,4], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Rubio", xlab="Days", ylab="Change in Polling")
dev.off()











setwd("D:/Dropbox/Grad School Year 3 Term 1/Time Series/TS Project/PoP Conditional Accept")

pdf("Support_on_Media.pdf", height=12, width=8)
# png("Support_on_Media.png", height=12, width=8, unit="in", res=120)
par(mfcol=c(4,2))

plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(trump.irf$responses[,2,7], rev(trump.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(trump.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Trump", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(bush.irf$responses[,2,7], rev(bush.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(bush.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Bush", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(carson.irf$responses[,2,7], rev(carson.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(carson.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Carson", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(christie.irf$responses[,2,7], rev(christie.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(christie.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Christie", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(cruz.irf$responses[,2,7], rev(cruz.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(cruz.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Cruz", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(fiorina.irf$responses[,2,7], rev(fiorina.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(fiorina.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Fiorina", xlab="Days", ylab="Change in Polling")



plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(paul.irf$responses[,2,7], rev(paul.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(paul.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Paul", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(rubio.irf$responses[,2,7], rev(rubio.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(rubio.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Rubio", xlab="Days", ylab="Change in Polling")
dev.off()

setwd("D:/Dropbox/Grad School Year 3 Term 1/Time Series/TS Project/PoP Conditional Accept")

pdf("Support_on_Media.pdf", height=12, width=8)
# png("Support_on_Media.png", height=12, width=8, unit="in", res=120)
par(mfcol=c(4,2))

plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(trump.irf$responses[,2,7], rev(trump.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(trump.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Trump", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(bush.irf$responses[,2,7], rev(bush.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(bush.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Bush", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(carson.irf$responses[,2,7], rev(carson.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(carson.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Carson", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(christie.irf$responses[,2,7], rev(christie.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(christie.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Christie", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(cruz.irf$responses[,2,7], rev(cruz.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(cruz.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Cruz", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(fiorina.irf$responses[,2,7], rev(fiorina.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(fiorina.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Fiorina", xlab="Days", ylab="Change in Polling")



plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(paul.irf$responses[,2,7], rev(paul.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(paul.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Paul", xlab="Days", ylab="Change in Polling")


plot.new()
plot.window(xlim=c(1,5), ylim=c(-.5,.5))
polygon(x=c(1:5,5:1), y=c(rubio.irf$responses[,2,7], rev(rubio.irf$responses[,1,7])),
        border=NA, col=ggplot2::alpha("gray", .5))
lines(rubio.irf$responses[,3,7], lwd=2)
axis(1)
axis(2)
box()
abline(h=0, lty=2)
title(main="Rubio", xlab="Days", ylab="Change in Polling")
dev.off()



pdf("Trump.pdf", height=8, width=8)
# png("Trump.png", height=8, width=8, unit="in", res=120)
par(mfcol=c(3,3), mar= c(3, 4, 3, 2) + 0.1)
for(ii in 1:9){
  
  plot.new()
  
  if(ii %% 3 == 1){
    plot.window(xlim=c(1,5), ylim=c(-1,2.5))
  } else if(ii %% 3 == 2){
    plot.window(xlim=c(1,5), ylim=c(0,6))

  } else {
    plot.window(xlim=c(1,5), ylim=c(-1,20))

  }
  
  if(ii==1){
    title(main="Shock to:\nMedia",ylab="Media")
  } else if(ii==2){
    title(ylab="Interest")
  } else if(ii==3){
    title(ylab="Polling")
  } else if(ii==4){
    title(main="Shock to:\nInterest")
  } else if(ii==7){
    title(main="Shock to:\nPolling")
  }
  
  polygon(x=c(1:5,5:1), y=c(trump.irf$responses[,2,ii], rev(trump.irf$responses[,1,ii])),
          border=NA, col=ggplot2::alpha("gray", .5))
  lines(trump.irf$responses[,3,ii], lwd=2)
  axis(1)
  axis(2)
  box()
  abline(h=0, lty=2)
}
dev.off()
